﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;
using NhaThuocOnline.Entities;
using NhaThuocOnline.Repositories;
using NhaThuocOnline.ViewModels;

namespace NhaThuocOnline.Service
{
    public class HoaDonBanHangService
    {
        public void InsertOrUpdate(ViewModelsHoaDonBanHang hdbh)
        {
            var en = Mapper.Map<ViewModelsHoaDonBanHang, HoaDonBanHang>(hdbh);
            using (HoaDonBanHangRepository repo = new HoaDonBanHangRepository())
            {
                repo.InsertOrUpdate(en);
                repo.Save();
            }
        }
        public void Delete(int id)
        {
            using (HoaDonBanHangRepository repo = new HoaDonBanHangRepository())
            {
                repo.Delete(id);
                repo.Save();
            }
        }
        public List<ViewModelsHoaDonBanHang> GetAll()
        {
            using (HoaDonBanHangRepository repo = new HoaDonBanHangRepository())
            {
                return Mapper.Map<List<HoaDonBanHang>, List<ViewModelsHoaDonBanHang>>(repo.GetAll.ToList());
            }
        }
        public ViewModelsHoaDonBanHang GetHoaDonBanHangById(int id)
        {
            using (HoaDonBanHangRepository repo = new HoaDonBanHangRepository())
            {
                return Mapper.Map<HoaDonBanHang, ViewModelsHoaDonBanHang>(repo.GetAll.Where(bn => bn.Id == id).FirstOrDefault());
            }
        }
    }
}
